package week7;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class WalMart implements DAO{
	public User registere(){
		User user;
		String Name;
		String address;
		String Password;
		String Account;
		System.out.println("请输入用户名：");
		Name =Input.Next();
		System.out.println("请输入密码：");
		Password=Input.Next();
		System.out.println("请输入收货地址：");
		address=Input.Next();
		DAO dao=new WalMart(); 
		@SuppressWarnings("unchecked")
		ArrayList<User> users=(ArrayList<User>)dao.readObjectToFile();
		Account=""+(users.size()+1000000);
		user=new User(Name,address,Account,Password,0,1);
		user.writeObjectToFile(users);
		users.clear();
		return user;
	}
	
	
		public User signin(User user){
			if(user.getFlag()==0) {
				String Password;
				String Account;
				System.out.println("请输入帐号：");
				Account =Input.Next();
				System.out.println("请输入密码：");
				Password=Input.Next();
				User users=new User(null,null,Account,Password,0,0);
				users.readObjectToFile();
				return users;
			}
			else {
				System.out.println("你已经登入");
				return user;
			}
			
		}
		@Override
		public void writeObjectToFile(Object obj) {
			File file =new File("D:\\Users\\henry\\workspace\\ShoppingMall\\Userinfo.txt");
	        FileOutputStream out;
	        try {
	            out = new FileOutputStream(file);
	            ObjectOutputStream objOut=new ObjectOutputStream(out);
				objOut.writeObject(obj);
	            objOut.flush();
	            objOut.close();
	            System.out.println("write object success!");
	        } catch (IOException e) {
	            System.out.println("write object failed");
	            e.printStackTrace();
	        }
	    }
		
		@Override
		public Object readObjectToFile() {
		        Object temp=null;
		        File file =new File("D:\\Users\\henry\\workspace\\ShoppingMall\\Userinfo.txt");
		        FileInputStream in;
		        try {
		            in = new FileInputStream(file);
		            ObjectInputStream objIn=new ObjectInputStream(in);
		            temp=objIn.readObject();     
		            objIn.close();      
		            System.out.println("read object success!");
		        } catch (IOException e) {
		            System.out.println("read object failed");
		            e.printStackTrace();
		        } catch (ClassNotFoundException e) {
		            e.printStackTrace();
		        }
		        return temp;
		    
		}
		@Override
		public void diplayAll(Object user) {
			@SuppressWarnings("unchecked")
			ArrayList<User> users=(ArrayList<User>) user;
			for (User user2 : users) {
				System.out.println(user2);
			}
		}
}
